<script setup lang="ts">
import {onMounted, ref} from 'vue'
import { Allsku, saleSku, cancelSale, getSkuInfo, removeSku} from '@/network/product/sku';
import type { SkuResponseData, SkuData, SkuInfoData } from '@/network/product/sku/type';
import { ElMessage } from 'element-plus';
    // 当前页面
    const pageNO = ref<number>(1)
    // 每一页展示数据条数
    const limit = ref<number>(3)
    const total = ref<number>(0)
    const skuArr = ref<SkuData[]>([])

    async function getSku(page = 1) {
        pageNO.value = page
        const res: SkuResponseData = await Allsku(pageNO.value, limit.value)
        // console.log(res);
        if (res.code === 200) {
            skuArr.value = res.data.records
            total.value = res.data.total
        }
    }
    onMounted(() => {
        getSku()
    })

    // 条数
    function handler() {
        getSku()
    }

    // 商品上架与下架
   async function updateSale(row: SkuData) {
    // isSale为1时，表示商品正在上架
        if (row.isSale === 1) {
          const res = await cancelSale(row.id)
          console.log(res);
          if (res.code === 200) {
            ElMessage({
                type: 'success',
                message: '下架成功'
            })
            getSku(pageNO.value)
          } else {
            ElMessage({
                type: 'error',
                message: '下架失败'
            })
          }
            
        } else {
            const res = await saleSku(row.id)
            console.log(res);
            if (res.code === 200) {
                ElMessage({
                    type: 'success',
                    message: '上架成功'
                })
                getSku(pageNO.value)
            } else {
                ElMessage({
                    type: 'error',
                    message: '上架失败'
                })
            }
            
        }
    }

    // 更新
    function updateSku() {
        ElMessage({
            type: 'success',
            message: '程序员正在开发中......'
        })
    }

    // 控制抽屉显示与隐藏
    const drawer = ref<boolean>(false)

    // 查看商品详情按钮
    const skuInfo = ref<SkuData>({
        category3Id: '',
        spuId: '',
        tmId: '', // 品牌id
        skuName: '',
        price: '',
        weight: '',
        skuDesc: '',
        skuAttrValueList: [],
        skuSaleAttrValueList: [], // 销售属性
        skuDefaultImg: '',
        isSale: 0, // 控制商品上架与下架
        id: 0 ,// 商品id
        skuImageList: []
    })
    // 展示商品详情
   async function findSku(row: SkuData) {
        //   抽屉显示
        drawer.value = true  
        
      const res: SkuInfoData = await getSkuInfo(row.id)
      console.log(res);
      if (res.code === 200) {
        skuInfo.value = res.data
      }
    
    }

    // 删除sku
   async function deleteSku(row: SkuData) {
        const res = await removeSku(row.id)
        console.log(res);
        if (res.code === 200) {
            ElMessage({
                type: 'success',
                message: '删除成功'
            })
            getSku(skuArr.value.length > 1 ? pageNO.value : pageNO.value - 1)
        } else {
            ElMessage({
                type: 'error',
                message: '删除失败'
            })
        }
    }
</script>


<template>
    <el-card>
        <el-table border style="margin: 20px 0;" :data="skuArr">
            <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
            <el-table-column prop="skuName" label="名称" show-overflow-tooltip width="150px" ></el-table-column>
            <el-table-column prop="skuDesc" label="描述" width="150px"></el-table-column>
            <el-table-column label="默认图片" width="150px">
                <template #="{row, $index}">
                    <el-image :src="row.skuDefaultImg" style="width: 100px; height: 100px;"></el-image>
                </template>
            </el-table-column>
            <el-table-column prop="weight" label="重量(克)" width="150px"></el-table-column>
            <el-table-column prop="price" label="价格(元)" width="150px"></el-table-column>
            <el-table-column label="操作" width="400px" fixed="right">
                <template #="{row, $index}">
                    <el-button v-has="'btn.Sku.updown'" @click="updateSale(row)" type="primary" size="small" :icon="row.isSale === 1 ? 'Bottom' : 'Top'"></el-button>
                    <el-button v-has="'btn.Sku.update'" @click="updateSku" type="primary" size="small" icon="Edit"></el-button>
                    <el-button v-has="'btn.Sku.detail'" @click="findSku(row)" type="primary" size="small" icon="InfoFilled"></el-button>
                    <el-popconfirm @confirm="deleteSku(row)" :title="`确定要删除${row.skuName}吗？`" width="200px">
                        <template #reference>
                            <el-button v-has="'btn.Sku.remove'" type="primary" size="small" icon="Delete"></el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
        v-model:current-page="pageNO"
        v-model:page-size="limit"
        :page-sizes="[3, 5, 7, 9]"
        :background="true"
        layout=" prev, pager, next, jumper, ->, total, sizes"
        :total="total"
        @current-change="getSku"
        @size-change="handler"
        />
        <!-- 抽屉组件，展示商品详情 -->
        <el-drawer v-model="drawer" direction="rtl">
            <template #header>
            <h4>查看商品详情</h4>
            </template>
            <template #default>
                <el-row style="margin: 10px 0;">
                    <el-col :span="6">名称</el-col>
                    <el-col :span="18">{{skuInfo.skuName}}</el-col>
                </el-row>
                <el-row style="margin: 10px 0;">
                    <el-col :span="6">描述</el-col>
                    <el-col :span="18">{{skuInfo.skuDesc}}</el-col>
                </el-row>
                <el-row style="margin: 10px 0;">
                    <el-col :span="6">价格</el-col>
                    <el-col :span="18">{{skuInfo.price}}</el-col>
                </el-row>
                <el-row style="margin: 10px 0;">
                    <el-col :span="6">平台属性</el-col>
                    <el-col :span="18">
                        <el-tag style="margin: 5px;" v-for="item in skuInfo.skuAttrValueList" :key="item.id">{{item.valueName}}</el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin: 10px 0;">
                    <el-col :span="6">销售属性</el-col>
                    <el-col :span="18">
                        <el-tag type="danger" style="margin: 5px;" v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id">{{item.saleAttrValueName}}</el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin: 10px 0;">
                    <el-col :span="6">商品图片</el-col>
                    <el-col :span="18">
                        <el-carousel :interval="4000" type="card" height="200px">
                            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                                <el-image :src="item.imgUrl"></el-image>
                            </el-carousel-item>
                        </el-carousel>
                    </el-col>
                </el-row>
            </template>
        </el-drawer>
    </el-card>
</template>


<style scoped lang='scss'>
    .el-carousel__item h3 {
        color: #475669;
        opacity: 0.75;
        line-height: 200px;
        margin: 0;
        text-align: center;
    }

    .el-carousel__item:nth-child(2n) {
        background-color: #99a9bf;
    }

    .el-carousel__item:nth-child(2n + 1) {
        background-color: #d3dce6;
    }
</style>